package com.example.myapplication

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import kotlin.random.Random

class MainActivity : AppCompatActivity() {

    private lateinit var content : EditText     //ALT+ENTER
    private lateinit var tips : TextView
    private lateinit var confirm : Button
    private lateinit var restart : Button

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        var count = 0;
        var randomNumber = Random.nextInt(1,100)

        content = findViewById(R.id.editTextNumber)
        tips = findViewById(R.id.textView)
        confirm = findViewById(R.id.button)
        restart = findViewById(R.id.button2)

        confirm.setOnClickListener {
            Log.d("MainActivity",content.text.toString())
            Toast.makeText(this,content.text,Toast.LENGTH_LONG).show()
            when{
                content.text.toString().toInt() > randomNumber-> {tips.text = "${tips.text}\n第${++count}次猜测数字是${content.text.toString()},偏大"}
                content.text.toString().toInt() < randomNumber -> {tips.text = "${tips.text}\n第${++count}次猜测数字是${content.text.toString()},偏小"}
                else -> {tips.text = "${tips.text}\n恭喜你猜对了"}
            }

        }

        restart.setOnClickListener {
            randomNumber = Random.nextInt(1,100)
            tips.text = "欢迎继续游戏"
        }
    }
}